package com.algoshow.controller;

import com.algoshow.model.SortData;
import com.algoshow.view.SecondMenu;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * @author vvv
 * @date 2023-05-17 13 04
 * discription
 */
public class SortPage extends BasePage {

    private List<SortData> sortDataList;

    // 监控的变量
    public Integer i = 0;
    public Integer j = 0;

    public SortPage(List<SortData> sortDataList, String algoName) throws HeadlessException {
        this.sortDataList = sortDataList;
        super.algoName = algoName;

        firstMenuButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (timer != null && timer.isAlive()) {
                    timer.stop();
                }
                dispose();
                new SecondMenu().showTest2();
            }
        });
    }

    @Override
    public void drawAlgo(Graphics g) {
        drawSort(g);
    }

    @Override
    public void drawParam(Graphics g) {
        // 坐标
        int x = paramX, y = paramY;
        System.out.println("test" + " " + i + " " + j);
        g.drawString("i = ", x, y);
        g.drawString(i.toString(), x + 35, y);
        g.drawString("j = ", x, y + 40);
        g.drawString(j.toString(), x + 35, y + 40);
    }

    private void drawSort(Graphics g) {
        if (sortDataList == null) {
            return;
        }
        //System.out.println(sortDataList);
        for (SortData data : sortDataList) {
            g.setColor(data.getColor());
            g.fillRect(data.getX(), startY, 30, data.getNum() * -50);
            g.setColor(Color.BLACK);
            g.drawString(Integer.toString(data.getNum()), data.getX() + 8, startY + 30);
        }
    }
    @Override
    public void startThread() {
        timer = new Thread() {
            @Override
            public void run() {
                switch (algoName) {
                    case "bubble" :
                        bubbleSort();
                        break;
                    case "insert" :
                        insertSort();
                        break;
                    case "fast":
                        fastSort(0, sortDataList.size() - 1);
                        break;
                    case "select":
                        selectSort();
                        break;
                    default:
                        timer.stop();
                }
            }
        };
        timer.start();
    }

    // 选择排序
    private void selectSort() {
        render(0);
        render(1);
        for (i = 0; i < sortDataList.size(); i++) {
            int minIndex = i;
            sortDataList.get(minIndex).setColor(red);
            render(3);
            for (j = i + 1; j < sortDataList.size(); j++) {
                //System.out.println("test" + " " + i + " " + j);
                render(2);
                sortDataList.get(j).setColor(GREEN);
                render(3);
                if (sortDataList.get(j).getNum() < sortDataList.get(minIndex).getNum()) {
                    sortDataList.get(j).setColor(red);
                    sortDataList.get(minIndex).setColor(blur);
                    minIndex = j;
                    render(4);
                } else {
                    sortDataList.get(j).setColor(blur);
                }
            }
            swap(i, minIndex);
            render(7);
            sortDataList.get(i).setColor(yellow);
            if (i != minIndex) {
                sortDataList.get(minIndex).setColor(blur);
            }
        }
        render(8);
    }

    // TODO 快速排序
    private void fastSort(int low, int high) {
        int temp,t;
        if(low>high){
            render(1);
            render(2);
            return;
        }
        i=low;
        j=high;
        //temp就是基准位
        temp = sortDataList.get(low).getNum();
        sortDataList.get(low).setColor(yellow);

        while (i<j) {
            //先看右边，依次往左递减
            while (temp <= sortDataList.get(j).getNum() && i<j) {
                sortDataList.get(j).setColor(GREEN);
                render(3);
                render(4);
                j--;
                render(5);
            }

            //再看左边，依次往右递增
            while (temp >= sortDataList.get(i).getNum() && i<j) {
                sortDataList.get(i).setColor(red);
                render(6);
                render(7);
                i++;
                render(8);
            }

            //如果满足条件则交换
            if (i<j) {
                render(9);
                render(10);
                swap(i, j);

            }
        }
        //最后将基准为与i和j相等位置的数字交换
        swap(low, i);
        sortDataList.get(i).setColor(yellow);
        render(12);

        //递归调用左半数组
        fastSort(low, j-1);
        render(13);

        //递归调用右半数组
        fastSort(j+1, high);
        render(14);
    }
    // 插入排序
    private void insertSort() {
        render(0);
        for (i = 0; i < sortDataList.size(); i++) {
            int temp = sortDataList.get(i).getNum();
            //int j;
            sortDataList.get(i).setColor(red);
            render(1);
            render(3,300);
            for (j = i; j > 0 && temp < sortDataList.get(j - 1).getNum(); j--) {
                sortDataList.get(j - 1).setColor(GREEN);
                render(3);

                swap(j, j - 1);
                sortDataList.get(j).setColor(yellow);
                sortDataList.get(j - 1).setColor(red);
                render(4);

            }

            sortDataList.get(j).setNum(temp);
            sortDataList.get(j).setColor(yellow);
            render(6);

        }
    }
    // 冒泡排序
    private void bubbleSort() {
        int len = sortDataList.size();
        render(0);
        for (i = 0; i < len; i++) {
            render(1);
            for (j = 0; j < len - i - 1; j++) {
                SortData data1 = sortDataList.get(j);
                SortData data2 = sortDataList.get(j + 1);
                data1.setColor(GREEN);
                data2.setColor(GREEN);
                render(2);
                if (data1.getNum() > data2.getNum()) {

                    int tempNum = data1.getNum();
                    data1.setNum(data2.getNum());
                    data2.setNum(tempNum);
                    render(3);
                }

                data1.setColor(blur);
                data2.setColor(blur);
            }
            sortDataList.get(len - 1 - i).setColor(Color.ORANGE);

        }
        render(0);
    }
    private void swap(int i, int j) {
        SortData data1 = sortDataList.get(i);
        SortData data2 = sortDataList.get(j);
        int tempNum = data1.getNum();
        data1.setNum(data2.getNum());
        data2.setNum(tempNum);
    }//元素交换

    private void debug() {
        for (SortData sortData : sortDataList) {
            System.out.println(sortData);
        }
    }
}
